from Core.exception import *
from Core.models import *


def pwd_encrypt(pwd):
    return pwd


def session_verify_id_s(session_id, session: dict):
    if session_id not in session:
        raise SessionNotExist(session_id)
        # raise Exception('session id [{}] is not exist!'.format(session_id))


def cookie_verify_c_database(cookies: dict):
    session_id = ''
    user_id = ''
    if 'sessionid' not in cookies:
        raise NoCookiesProvide()
    else:
        session_id = cookies['sessionid']
        user_id = cookies['userid']

    session_list = Session.objects.filter(id=session_id)
    if not session_list.exists():
        raise SessionNotExist(session_id)
    else:
        session_info = session_list.first()
        user = session_info.user
        if user_id != user.id:
            raise SessionIsInvalid(session_id)


def cookie_verify_c_s(cookies: dict, session: dict):
    # session_id = ''
    if 'sessionid' not in cookies:
        raise NoCookiesProvide()
    else:
        session_id = cookies['sessionid']

    if session_id not in session:
        raise SessionNotExist(session_id)


def cookie_verify(request):
    cookie_verify_c_database(request.COOKIES)
